package cn.jly.boot.kafka.controller;

import cn.jly.boot.kafka.bean.User;
import cn.jly.boot.kafka.service.KafkaProducer;
import cn.jly.boot.kafka.utils.RequestParam;
import cn.jly.boot.kafka.utils.ResultBase;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @PackageName cn.jly.boot.kafka.controller
 * @ClassName KafkaController
 * @Description kafka控制器
 * @Author 姬岚洋
 * @Date 2021/1/10 下午 9:32
 */
@Controller
@RequestMapping("/kafka")
public class KafkaController {
    @Autowired
    KafkaProducer kafkaProducer;

    @ResponseBody
    @PostMapping("/sendMessage")
    public ResultBase<String> sendMessage(@RequestBody RequestParam<User> requestParam) {
        final ResultBase<String> resultBase = new ResultBase<>();
        kafkaProducer.sendMessage(requestParam.getTopic(), JSON.toJSONString(requestParam.getData()));
        resultBase.setCode(200);
        resultBase.setMessage("success");
        resultBase.setData(JSON.toJSONString(requestParam.getData()));
        return resultBase;
    }

    @ResponseBody
    @PostMapping("/sendMessageWithCallback")
    public ResultBase<String> sendMessageWithCallback(@RequestBody RequestParam<User> requestParam) {
        final ResultBase<String> resultBase = new ResultBase<>();
        kafkaProducer.sendMessageWithCallback(requestParam.getTopic(), JSON.toJSONString(requestParam.getData()));
        resultBase.setCode(200);
        resultBase.setMessage("success");
        resultBase.setData(JSON.toJSONString(requestParam.getData()));
        return resultBase;
    }
}
